//缓存以及权限相关

//设置图片验证码key
export function setCaptchaKey(key) {
	localStorage.removeItem("zjf-erp-captcha-key");
	localStorage.setItem("zjf-erp-captcha-key", key);
}

//获取图片验证码key值
export function getCaptchaKey() {
	return localStorage.getItem("zjf-erp-captcha-key");
}

//获取token
export function getToken() {
	return localStorage.getItem("zjf-erp-token") || "";
}

//设置token
export function setToken(token) {
	localStorage.setItem("zjf-erp-token", token);
}

//获取当前用户
export function getCurrentUser() {
	return JSON.parse(localStorage.getItem("zjf-erp-current-user")) || {};
}

//设置当前用户
export function setCurrentUser(account) {
	localStorage.setItem("zjf-erp-current-user", JSON.stringify(account));
}

//获取当前用户信息（区分主子账号）
export function getDifferUserDetails() {
	return JSON.parse(localStorage.getItem("zjf-erp-differ-user")) || {};
}

//设置当前用户信息（区分主子账号）
export function setDifferUserDetails(account) {
	localStorage.setItem("zjf-erp-differ-user", JSON.stringify(account));
}

//获取当前用户账户标识
export function getCurrentUserType() {
	return JSON.parse(localStorage.getItem("zjf-erp-current-user-type")) || {};
}

//设置当前用户账户标识
export function setCurrentUserType(account) {
	localStorage.setItem("zjf-erp-current-user-type", JSON.stringify(account));
}

//获取erp一些基础配置
export function getBaseData(key) {
	const baseData = JSON.parse(localStorage.getItem("zjf-erp-base-Data")) || {};
	if (baseData[key]) {
		return baseData[key];
	}
}

//设置erp一些基础配置
export function setBaseData(key, value) {
	const baseData = JSON.parse(localStorage.getItem("zjf-erp-base-Data")) || {};
	localStorage.setItem(
		"zjf-erp-base-Data",
		JSON.stringify({
			...baseData,
			[key]: value
		})
	);
}

//登出，清除所有用户信息
export function removeAll() {
	localStorage.removeItem('zjf-erp-authority');
  localStorage.removeItem('zjf-erp-token');
  localStorage.removeItem('zjf-erp-current-user');
  localStorage.removeItem('zjf-erp-differ-user');//（可以区分主/子账号的当前用户对象）
  localStorage.removeItem('zjf-erp-captcha-key');
  localStorage.removeItem('zjf-erp-store-list');
  localStorage.removeItem('zjf-erp-announce-hasRead');
  sessionStorage.removeItem('isLock');
}

/**
 * @description js原生设置cookie
 * @param {String} name 给你要设置的cookie起个名字（key）
 * @param {String} value cookie的具体内容(value)
 * @param {String} expiredays 设置cookie的过期时间，单位：天
 */

export function setCookie(name, value, expiredays = 1) {
	const t = new Date();
	t.setDate(t.getDate() + expiredays);
	t.setHours(0);
	t.setMinutes(0);
	t.setSeconds(0);
	document.cookie = name + "=" + escape(value) + ";expires=" + t.toGMTString();
}

/**
 * @description js原生获取cookie方法
 * @param {String} name 你要获取的cookie名
 */
export function getCookie(name) {
	if (document.cookie.length > 0) {
		const start = document.cookie.indexOf(name + "=");
		if (start !== -1) {
			return true;
		}
		return false;
	}
	return false;
}

export function getCookieValue(name) {
	if (document.cookie.length > 0) {
		const el = document.cookie.split(";").find(item => item.indexOf(name + "=") > -1);
		if (el) {
			return el.split("=")[1];
		}
	}
	return "";
}

// //获取打开tab
// export function getTabStorage() {
//   return sessionStorage.getItem('zjf-erp-tabList') ? JSON.parse(sessionStorage.getItem('zjf-erp-tabList')) : undefined;
// }

// //设置打开tab
// export function setTabStorage(data) {
//   sessionStorage.setItem('zjf-erp-tabList', data);
// }

//设置打印机的名字
export function setPrinterName(name) {
	if (name) {
		localStorage.removeItem("zjf-erp-printer-name");
		localStorage.setItem("zjf-erp-printer-name", name);
	}
}

//获取打印机的名字
export function getPrinterName() {
	return localStorage.getItem("zjf-erp-printer-name");
}

//设置打印机的名字(公共)
export function setAllPrinterName(obj) {
	if (obj) {
		localStorage.removeItem("common-printer-memory");
		localStorage.setItem("common-printer-memory", JSON.stringify(obj));
	}
}

//获取打印机的名字(公共)
export function getAllPrinterName() {
	return JSON.parse(localStorage.getItem("common-printer-memory")) || {};
}

//获取合格证打印机的名字
export function getCardPrinter() {
	return localStorage.getItem("zjf-erp-card-printer-name");
}

//合格证打印机
export function setCardPrinter(name) {
	if (name) {
		localStorage.removeItem("zjf-erp-card-printer-name");
		localStorage.setItem("zjf-erp-card-printer-name", name);
	}
}

//手工下单：设置默认店铺
export function setDefaultStore(obj) {
	if (Object.keys(obj).length) {
		localStorage.removeItem("zjf-erp-store-id");
		localStorage.setItem("zjf-erp-store-id", JSON.stringify(obj));
	}
}

//手工下单：获取默认店铺
export function getDefaultStore() {
	return JSON.parse(localStorage.getItem("zjf-erp-store-id"));
}

//设置店铺搜索历史列表
export function setStoreList(arr) {
	if (arr) {
		localStorage.removeItem("zjf-erp-store-list");
		localStorage.setItem("zjf-erp-store-list", JSON.stringify(arr));
	}
}

//获取店铺搜索历史列表
export function getStoreList() {
	return localStorage.getItem("zjf-erp-store-list");
}

//获取授权回调信息
export function getAuthMsg() {
	return localStorage.getItem("zjf-erp-auth-msg") || undefined;
}

//储存授权回调信息
export function setAuthMsg(data) {
	localStorage.setItem("zjf-erp-auth-msg", JSON.stringify(data));
}

//删除授权回调信息
export function removeAuthMsg() {
	localStorage.removeItem("zjf-erp-auth-msg");
}

//获取打印机和快递公司的绑定关系
export function getPrinterRelativeExpress() {
	return JSON.parse(localStorage.getItem("printer-relative-express")) || {};
}

//修改打印机和快递公司的绑定关系
export function setPrinterRelativeExpress(data) {
	localStorage.setItem("printer-relative-express", JSON.stringify(data));
}

//获取快递公司和打印机的绑定关系
export function getExpressRelativePrinter() {
	return JSON.parse(localStorage.getItem("express-relative-printer")) || {};
}

//设置快递公司和打印机的绑定关系
export function setExpressRelativePrinter(data) {
	localStorage.setItem("express-relative-printer", JSON.stringify(data));
}

//公告已读（关闭）状态
export function getAnnounceStatus() {
	return localStorage.getItem("zjf-erp-announce-hasRead") || "";
}

//公告已读（关闭）状态
export function setAnnounceStatus(token) {
	localStorage.setItem("zjf-erp-announce-hasRead", token);
}

//获取页面配置项
export function getGlobalConfig(keys) {
	return localStorage.getItem(keys) ? JSON.parse(localStorage.getItem(keys)) : {};
}

//设置页面配置项
export function setGlobalConfig(keys, data) {
	localStorage.setItem(keys, JSON.stringify(data));
}

//校验是否是生产环境
export function checkIsProd() {
	const hostname = window.location.hostname;
	const whiteHost = ['test', 'localhost', 'stage'];
	for(let i in whiteHost) {
		if(hostname.includes(whiteHost[i])) {
			return false;
		}
	}
	return true;
}

//是否是开通代拿的用户id
export function isDianaUser(uuid) {
  const uuids = [3000181, 300051, 189593, 284416, 350091, 690665, 392568, 351557, 705306, 650050, 211397, 672731, 159685, 679540, 712318, 667197, 486660, 289402, 103232, 400990, 508066, 42073, 705357, 400779, 631154, 661592, 713889, 627218, 372411, 661870, 372411, 711674, 699187, 715048, 48219, 116517, 634171, 154867, 389831, 698490, 384205, 642694, 716571, 631869, 393201, 670131, 715546, 716186, 155631, 57901];
  return uuids.includes(uuid);
}
